package android.test.devicetest;

import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.test.getintent.GetIntent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.test.until.Max3HWVerion;

public class RGB extends Activity {

    private int num = 1;
    private int currentValue;
    private static final int MAX_BRIGHTNESS = 255;
    private TextView rgbTest;
    private TextView toast;
    private TextView display_brightness;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rgb_info);

        currentValue = Settings.System.getInt(getBaseContext()
                .getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,
                currentValue);

        display_brightness = (TextView) findViewById(R.id.atoast);
        toast = (TextView) findViewById(R.id.toast);
        rgbTest = (TextView) findViewById(R.id.rgb_test);
    }

    private void toNext() {
        num++;
        if (num == 2) {
            rgbTest.setBackgroundColor(Color.GREEN);
            toast.setBackgroundColor(Color.GREEN);
            display_brightness.setBackgroundColor(Color.GREEN);
        }
        if (num == 3) {
            rgbTest.setBackgroundColor(Color.BLUE);
            toast.setBackgroundColor(Color.BLUE);
            display_brightness.setBackgroundColor(Color.BLUE);
        }
        if (num == 4) {
            rgbTest.setBackgroundColor(Color.BLACK);
            toast.setBackgroundColor(Color.BLACK);
            display_brightness.setBackgroundColor(Color.BLACK);
        }
        if (num == 5) {
            rgbTest.setText("press LEFT to decrease\npress RIGHT to increase");
            rgbTest.setBackgroundColor(Color.WHITE);
            toast.setBackgroundColor(Color.WHITE);
            display_brightness.setBackgroundColor(Color.WHITE);
            display_brightness.setText(String.valueOf("brightness: " + currentValue));
        }
        if (num == 6) {
            rgbTest.setVisibility(View.GONE);
            toast  .setVisibility(View.GONE);
            display_brightness.setBackgroundDrawable(getResources().getDrawable(R.drawable.colorbar));
            display_brightness.setText("");
        }
        if (num == 7) {
            finish();
            Intent mIntent = GetIntent.getItemIntent(RGB.this, "led");
            if (mIntent != null) {
                startActivity(mIntent);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (num == 5 || num == 6) {
                currentValue = currentValue - 10;
                if (currentValue < 1) {
                    currentValue = 1;
                }
                display_brightness.setText(String.valueOf("brightness: " + currentValue));
                updateBrightnessSeekBar();
                Max3HWVerion.setExtLcdBacklight(-1, 0);
            }
            break;

        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (num == 5 || num == 6) {
                currentValue = currentValue + 10;
                if (currentValue > MAX_BRIGHTNESS) {
                    currentValue = MAX_BRIGHTNESS;
                }
                display_brightness.setText(String.valueOf("brightness: " + currentValue));
                updateBrightnessSeekBar();
                Max3HWVerion.setExtLcdBacklight( 1, 0);
            }
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (num == 5 || num == 6) {
                Max3HWVerion.setExtLcdBacklight(-1, 1);
            }
            break;

        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (num == 5 || num == 6) {
                Max3HWVerion.setExtLcdBacklight( 1, 1);
            }
            break;

        case KeyEvent.KEYCODE_ENTER:
            if (num <= 7) {
                toNext();
            }
            break;

        case KeyEvent.KEYCODE_BACK:
            finish();
            System.exit(0);
            break;
        }
        return super.onKeyUp(keyCode, event);
    }

    private void updateBrightnessSeekBar() {
        // for min backlight
        if (currentValue < 1)
            currentValue = 1;

        android.provider.Settings.System.putInt(getContentResolver(),
                android.provider.Settings.System.SCREEN_BRIGHTNESS,
                currentValue);

        WindowManager.LayoutParams winParams = getWindow().getAttributes();

        Float tmpFloat = (float) currentValue / 255;
        if (0.000 < tmpFloat && tmpFloat < 1) {
            winParams.screenBrightness = tmpFloat;
        }

        getWindow().setAttributes(winParams);
    }
}
